Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports SQLtoASPX.Support
Imports System.Text

Public Class DragDropListMasterASPX

    Private TableGen As RawCreateScript
    Private mDatabaseName As String
    Public TableName As String

    Public Sub New(ByVal TableGen As RawCreateScript)
        Me.TableGen = TableGen
    End Sub

    Public Function ToString(ByVal ComboBoxTable As String) As String
        Dim sb As New StringBuilder
        TableGen.TableName = TableName
        GetFirstAtLineASPX(sb, TableGen)
        sb.AppendLine("<%@ Import Namespace=""" & ComboBoxTable & "HTMLUnorderedList"" %>")
        sb.AppendLine("<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">")
        sb.AppendLine("<html xmlns=""http://www.w3.org/1999/xhtml"" >")
        sb.AppendLine("<head runat=""server"">")
        sb.AppendLine("    <title>" & ComboBoxTable & "</title>")
        sb.AppendLine("    <style type=""text/css"" >")
        sb.AppendLine("        input.Hidden")
        sb.AppendLine("        {")
        sb.AppendLine("            display:none;")
        sb.AppendLine("        }")
        sb.AppendLine("        body ")
        sb.AppendLine("        {")
        sb.AppendLine("	        margin:0;")
        sb.AppendLine("	        padding:0;")
        sb.AppendLine("        }")
        sb.AppendLine("    </style>")
        sb.AppendLine("    <link rel=""stylesheet"" type=""text/css"" href=""http://yui.yahooapis.com/2.5.1/build/fonts/fonts-min.css"" />")
        sb.AppendLine("    <script src=""http://yui.yahooapis.com/2.5.1/build/yahoo-dom-event/yahoo-dom-event.js"" type=""text/javascript""></script>")
        sb.AppendLine("    <script src=""http://yui.yahooapis.com/2.5.1/build/animation/animation-min.js"" type=""text/javascript""></script>")
        sb.AppendLine("    <script src=""http://yui.yahooapis.com/2.5.1/build/dragdrop/dragdrop-min.js"" type=""text/javascript""></script>")
        sb.AppendLine("")
        sb.AppendLine("    <style type=""text/css"">")
        sb.AppendLine("        div.workarea { padding:10px; float:left }")
        sb.AppendLine("        ul.draglist { ")
        sb.AppendLine("            position: relative;")
        sb.AppendLine("            width: 200px; ")
        sb.AppendLine("            height:1200px;")
        sb.AppendLine("            background: #f7f7f7;")
        sb.AppendLine("            border: 1px solid gray;")
        sb.AppendLine("            list-style: none;")
        sb.AppendLine("            margin:0;")
        sb.AppendLine("            padding:0;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        ul.draglist li {")
        sb.AppendLine("            margin: 1px;")
        sb.AppendLine("            cursor: pointer; ")
        sb.AppendLine("                font-size:8pt;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        ul.draglist_alt { ")
        sb.AppendLine("            position: relative;")
        sb.AppendLine("            width: 200px; ")
        sb.AppendLine("            list-style: none;")
        sb.AppendLine("            margin:0;")
        sb.AppendLine("            padding:0;")
        sb.AppendLine("            /*")
        sb.AppendLine("               The bottom padding provides the cushion that makes the empty ")
        sb.AppendLine("               list targetable.  Alternatively, we could leave the padding ")
        sb.AppendLine("               off by default, adding it when we detect that the list is empty.")
        sb.AppendLine("            */")
        sb.AppendLine("            padding-bottom:20px;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        ul.draglist_alt li {")
        sb.AppendLine("            margin: 1px;")
        sb.AppendLine("            cursor: pointer; ")
        sb.AppendLine("            font-size:8pt;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("")
        sb.AppendLine("        li.list1 {")
        sb.AppendLine("            background-color: #D1E6EC;")
        sb.AppendLine("            border:1px solid #7EA6B2;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        li.list2 {")
        sb.AppendLine("            background-color: #D8D4E2;")
        sb.AppendLine("            border:1px solid #6B4C86;")
        sb.AppendLine("            font-size:8pt;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        #user_actions { float: right; }")
        sb.AppendLine("</style>")
        sb.AppendLine("    ")
        sb.AppendLine("<script type=""text/javascript"">")
        sb.AppendLine("")
        sb.AppendLine("function SaveSelected()")
        sb.AppendLine("{")
        sb.AppendLine("    var Dom = YAHOO.util.Dom;")
        sb.AppendLine("    var ul1 = Dom.get(""ul1"");")
        sb.AppendLine("    var txtResult = Dom.get(""txtResult"");")
        sb.AppendLine("    txtResult.value = parseList2(ul1);")
        sb.AppendLine("}")
        sb.AppendLine("")
        sb.AppendLine("var parseList2 = function(ul) {")
        sb.AppendLine("    var items = ul.getElementsByTagName(""li"");")
        sb.AppendLine("    var out = """";")
        sb.AppendLine("    for ( i=0;i<items.length;i=i+1 ) {")
        sb.AppendLine("        out += items[i].id + "","";")
        sb.AppendLine("    }")
        sb.AppendLine("    return out;")
        sb.AppendLine("};")
        sb.AppendLine("")
        sb.AppendLine("")
        sb.AppendLine("")
        sb.AppendLine("")
        sb.AppendLine("(function() {")
        sb.AppendLine("")
        sb.AppendLine("var Dom = YAHOO.util.Dom;")
        sb.AppendLine("var Event = YAHOO.util.Event;")
        sb.AppendLine("var DDM = YAHOO.util.DragDropMgr;")
        sb.AppendLine("")
        sb.AppendLine("//////////////////////////////////////////////////////////////////////////////")
        sb.AppendLine("// example app")
        sb.AppendLine("//////////////////////////////////////////////////////////////////////////////")
        sb.AppendLine("YAHOO.example.DDApp = {")
        sb.AppendLine("    init: function() {")
        sb.AppendLine("    ")
        sb.AppendLine("    <%= UnassignedListItems %>")
        sb.AppendLine("    ")
        sb.AppendLine("    <%= AssignedListItems %>")
        sb.AppendLine("    ")
        sb.AppendLine("        new YAHOO.util.DDTarget(""ul1"");")
        sb.AppendLine("        new YAHOO.util.DDTarget(""ul2"");")
        sb.AppendLine("")
        sb.AppendLine("    }")
        sb.AppendLine("};")
        sb.AppendLine("")
        sb.AppendLine("//////////////////////////////////////////////////////////////////////////////")
        sb.AppendLine("// custom drag and drop implementation")
        sb.AppendLine("//////////////////////////////////////////////////////////////////////////////")
        sb.AppendLine("")
        sb.AppendLine("YAHOO.example.DDList = function(id, sGroup, config) {")
        sb.AppendLine("")
        sb.AppendLine("    YAHOO.example.DDList.superclass.constructor.call(this, id, sGroup, config);")
        sb.AppendLine("")
        sb.AppendLine("    this.logger = this.logger || YAHOO;")
        sb.AppendLine("    var el = this.getDragEl();")
        sb.AppendLine("    Dom.setStyle(el, ""opacity"", 0.67); // The proxy is slightly transparent")
        sb.AppendLine("")
        sb.AppendLine("    this.goingUp = false;")
        sb.AppendLine("    this.lastY = 0;")
        sb.AppendLine("};")
        sb.AppendLine("")
        sb.AppendLine("YAHOO.extend(YAHOO.example.DDList, YAHOO.util.DDProxy, {")
        sb.AppendLine("")
        sb.AppendLine("    startDrag: function(x, y) {")
        sb.AppendLine("        this.logger.log(this.id + "" startDrag"");")
        sb.AppendLine("")
        sb.AppendLine("        // make the proxy look like the source element")
        sb.AppendLine("        var dragEl = this.getDragEl();")
        sb.AppendLine("        var clickEl = this.getEl();")
        sb.AppendLine("        Dom.setStyle(clickEl, ""visibility"", ""hidden"");")
        sb.AppendLine("")
        sb.AppendLine("        dragEl.innerHTML = clickEl.innerHTML;")
        sb.AppendLine("")
        sb.AppendLine("        Dom.setStyle(dragEl, ""color"", Dom.getStyle(clickEl, ""color""));")
        sb.AppendLine("        Dom.setStyle(dragEl, ""backgroundColor"", Dom.getStyle(clickEl, ""backgroundColor""));")
        sb.AppendLine("        Dom.setStyle(dragEl, ""border"", ""2px solid gray"");")
        sb.AppendLine("    },")
        sb.AppendLine("")
        sb.AppendLine("    endDrag: function(e) {")
        sb.AppendLine("")
        sb.AppendLine("        var srcEl = this.getEl();")
        sb.AppendLine("        var proxy = this.getDragEl();")
        sb.AppendLine("")
        sb.AppendLine("        // Show the proxy element and animate it to the src element's location")
        sb.AppendLine("        Dom.setStyle(proxy, ""visibility"", """");")
        sb.AppendLine("        var a = new YAHOO.util.Motion( ")
        sb.AppendLine("            proxy, { ")
        sb.AppendLine("                points: { ")
        sb.AppendLine("                    to: Dom.getXY(srcEl)")
        sb.AppendLine("                }")
        sb.AppendLine("            }, ")
        sb.AppendLine("            0.2, ")
        sb.AppendLine("            YAHOO.util.Easing.easeOut ")
        sb.AppendLine("        )")
        sb.AppendLine("        var proxyid = proxy.id;")
        sb.AppendLine("        var thisid = this.id;")
        sb.AppendLine("")
        sb.AppendLine("        // Hide the proxy and show the source element when finished with the animation")
        sb.AppendLine("        a.onComplete.subscribe(function() {")
        sb.AppendLine("                Dom.setStyle(proxyid, ""visibility"", ""hidden"");")
        sb.AppendLine("                Dom.setStyle(thisid, ""visibility"", """");")
        sb.AppendLine("            });")
        sb.AppendLine("        a.animate();")
        sb.AppendLine("    },")
        sb.AppendLine("")
        sb.AppendLine("    onDragDrop: function(e, id) {")
        sb.AppendLine("")
        sb.AppendLine("        // If there is one drop interaction, the li was dropped either on the list,")
        sb.AppendLine("        // or it was dropped on the current location of the source element.")
        sb.AppendLine("        if (DDM.interactionInfo.drop.length === 1) {")
        sb.AppendLine("")
        sb.AppendLine("            // The position of the cursor at the time of the drop (YAHOO.util.Point)")
        sb.AppendLine("            var pt = DDM.interactionInfo.point; ")
        sb.AppendLine("")
        sb.AppendLine("            // The region occupied by the source element at the time of the drop")
        sb.AppendLine("            var region = DDM.interactionInfo.sourceRegion; ")
        sb.AppendLine("")
        sb.AppendLine("            // Check to see if we are over the source element's location.  We will")
        sb.AppendLine("            // append to the bottom of the list once we are sure it was a drop in")
        sb.AppendLine("            // the negative space (the area of the list without any list items)")
        sb.AppendLine("            if (!region.intersect(pt)) {")
        sb.AppendLine("                var destEl = Dom.get(id);")
        sb.AppendLine("                var destDD = DDM.getDDById(id);")
        sb.AppendLine("                destEl.appendChild(this.getEl());")
        sb.AppendLine("                destDD.isEmpty = false;")
        sb.AppendLine("                DDM.refreshCache();")
        sb.AppendLine("            }")
        sb.AppendLine("")
        sb.AppendLine("        }")
        sb.AppendLine("    },")
        sb.AppendLine("")
        sb.AppendLine("    onDrag: function(e) {")
        sb.AppendLine("")
        sb.AppendLine("        // Keep track of the direction of the drag for use during onDragOver")
        sb.AppendLine("        var y = Event.getPageY(e);")
        sb.AppendLine("")
        sb.AppendLine("        if (y < this.lastY) {")
        sb.AppendLine("            this.goingUp = true;")
        sb.AppendLine("        } else if (y > this.lastY) {")
        sb.AppendLine("            this.goingUp = false;")
        sb.AppendLine("        }")
        sb.AppendLine("")
        sb.AppendLine("        this.lastY = y;")
        sb.AppendLine("    },")
        sb.AppendLine("")
        sb.AppendLine("    onDragOver: function(e, id) {")
        sb.AppendLine("    ")
        sb.AppendLine("        var srcEl = this.getEl();")
        sb.AppendLine("        var destEl = Dom.get(id);")
        sb.AppendLine("")
        sb.AppendLine("        // We are only concerned with list items, we ignore the dragover")
        sb.AppendLine("        // notifications for the list.")
        sb.AppendLine("        if (destEl.nodeName.toLowerCase() == ""li"") {")
        sb.AppendLine("            var orig_p = srcEl.parentNode;")
        sb.AppendLine("            var p = destEl.parentNode;")
        sb.AppendLine("")
        sb.AppendLine("            if (this.goingUp) {")
        sb.AppendLine("                p.insertBefore(srcEl, destEl); // insert above")
        sb.AppendLine("            } else {")
        sb.AppendLine("                p.insertBefore(srcEl, destEl.nextSibling); // insert below")
        sb.AppendLine("            }")
        sb.AppendLine("")
        sb.AppendLine("            DDM.refreshCache();")
        sb.AppendLine("        }")
        sb.AppendLine("    }")
        sb.AppendLine("});")
        sb.AppendLine("")
        sb.AppendLine("Event.onDOMReady(YAHOO.example.DDApp.init, YAHOO.example.DDApp, true);")
        sb.AppendLine("")
        sb.AppendLine("})();")
        sb.AppendLine("</script>")
        sb.AppendLine("</head>")
        sb.AppendLine("<body>")
        sb.AppendLine("    <form id=""form1"" runat=""server"">")
        sb.AppendLine("        <asp:DropDownList ID=""cbo" & ComboBoxTable & """ runat=""server"" AutoPostBack=""true""></asp:DropDownList>")
        sb.AppendLine("    <div>")
        sb.AppendLine("        <div class=""workarea"">")
        sb.AppendLine("            <h3>")
        sb.AppendLine("                Assigned</h3>")
        sb.AppendLine("            <%=AssignedList.ToString()%>")
        sb.AppendLine("        </div>")
        sb.AppendLine("        <div class=""workarea"">")
        sb.AppendLine("            <h3>")
        sb.AppendLine("                Unassinged</h3>")
        sb.AppendLine("                <%=UnassignedList.ToString()%>   ")
        sb.AppendLine("</div>")
        sb.AppendLine("        <asp:Button ID=""cmdSave"" runat=""server"" Text=""Save"" OnClientClick=""SaveSelected();"" /><br />")
        sb.AppendLine("        <asp:TextBox ID=""txtResult"" runat=""server"" CssClass=""Hidden""></asp:TextBox></div>")
        sb.AppendLine("    </form>")
        sb.AppendLine("</body>")
        sb.AppendLine("</html>")

        Return sb.ToString
    End Function
End Class
